<template>
  <div class="login">
    <div class="loginBox">
      <!-- 头像 -->
      <div class="avator">
        <img src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3985367431,1138839007&fm=26&gp=0.jpg" alt="" />
      </div>
      <!-- 输入框 -->
      <el-form ref="loginForm" :model="user" :rules="loginRules" class="loginInput">
        <el-form-item prop="username">
          <el-input
            v-model="user.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-monitor"
            v-model="user.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="verify">登录</el-button>
          <el-button type="info" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //重置用户名密码
    resetLogin(){
        this.$refs.loginForm.resetFields()
    },
    //提交验证表单
    verify(){
        this.$refs.loginForm.validate(async vaild =>{
            //console.log(vaild)
          let {data : res} = await this.$http.post("login",this.user)
          console.log(res)
          if(res.meta.status != 200) return this.$message.error("登录失败")
          this.$message.success("登录成功")

          //把token存储在sessionStorage中，跳转到/home
          sessionStorage.setItem("token",res.data.token)
          this.$router.push("/home")
        })
    }
  },
};
</script>

<style lang="less" scoped>
.login {
  background-color: lightsteelblue;
  height: 100%;

  .loginBox {
    width: 480px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avator {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      background-color: #eee;
      padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px #fff;
      border: 1px solid #fff;

      img {
        width: 100px;
        height: 100px;
        border-radius: 50%;
      }
    }

    .loginInput {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;

      .btn {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>